package com.briup.cms.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.briup.cms.bean.Slideshow;
import com.briup.cms.service.ISlideshowService;
import com.briup.cms.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "轮播图模块")
@RestController
@RequestMapping("/slideshow")
public class SlideshowController {
    @Autowired
    private ISlideshowService slideshowService;

    @ApiOperation(value = "根据id查询轮播图信息",notes = "用于更新时的数据回显")
    @GetMapping("/queryById/{id}")
    public Result getById(@PathVariable Integer id) {
        Slideshow slideshow = slideshowService.queryById(id);
        return Result.success(slideshow);
    }

    @ApiOperation(value = "查询所有启用的轮播图",notes = "按照状态查询")
    @GetMapping("/queryAllEnable")
    public Result queryAllEnable(){
        List<Slideshow> allEnable = slideshowService.queryAllEnable();
        return Result.success(allEnable);
    }

    @ApiOperation(value = "查询所有轮播图",notes = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页码",required = true, defaultValue="1"),
            @ApiImplicitParam(name = "pageSize",value = "每页大小",required = true, defaultValue="4"),
            @ApiImplicitParam(name = "status",value = "图片状态"),
            @ApiImplicitParam(name = "description",value = "描述")
    })
    @GetMapping("/query")
    public Result query(Integer pageNum,Integer pageSize,String status,String description){
        IPage<Slideshow> p = slideshowService.query(pageNum, pageSize, status, description);
        return Result.success(p);
    }

    //单个删除和批量删除共用一个接口
    @ApiOperation(value = "批量删除轮播图",notes = "需要提供多个id值")
    @DeleteMapping("/deleteByBatch/{ids}")
    public Result deleteSlideshowInBatch(@PathVariable("ids") List<Integer> ids){
        log.info("id:{}",ids);
        slideshowService.deleteInBatch(ids);
        return Result.success("删除成功");
    }

    @ApiOperation(value = "新增或修改轮播图",notes = "slideshow参数包含id值则为更新，不包含id为新增")
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody Slideshow slideshow){
        slideshowService.saveOrUpdate(slideshow);
        return Result.success("操作成功");
    }

}